/*用户信息-修改*/
$("#form-table-edit").validate({
  rules: {
    tableName: {
      required: true,
    },
  },
  focusCleanup: true
});

 function submitHandler() {
 	if ($.validate.form()) {
 		$.operate.saveTab(prefix + "/edit", $("#form-gen-edit").serializeArray());
 	}
 }
 var oper = getQueryString('oper') //当前弹窗类型
var tableId = getQueryString('id') //从列表传过来的tableId
 var prefix = api + "/gen/tool/gen";
 $(function() {
 	$('input[name=tableId]').val(tableId)
 	var options = {
 		url: prefix + "/column/list?tableId="+tableId,
 		sortName: "sort",
 		sortOrder: "desc",
 		height: $(window).height() - 166,
 		pagination: false,
 		showSearch: false,
 		showRefresh: false,
 		showToggle: false,
 		showColumns: false,
 		onLoadSuccess: onLoadSuccess,
 		onReorderRow: onReorderRow,
 		columns: [
 		  {
 				title: "序号",
 				width: "5%",
 				formatter: function(value, row, index) {
 					// 编号隐藏域
 					var columnIdHtml = $.common.sprintf("<input type='hidden' name='columns[%s].columnId' value='%s'>", index,
 						row.columnId);
 					// 排序隐藏域
 					var sortHtml = $.common.sprintf(
 						"<input type='hidden' name='columns[%s].sort' value='%s' id='columns_sort_%s'>", index, row.sort, row.columnId
 					);
 					return columnIdHtml + sortHtml + $.table.serialNumber(index);
 				},
 				cellStyle: function(value, row, index) {
 					return {
 						css: {
 							"cursor": "move"
 						}
 					};
 				}
 			},
 			{
 				field: 'columnName',
 				title: '字段列名',
 				width: "10%",
 				'class': "nodrag",
 				cellStyle: function(value, row, index) {
 					return {
 						css: {
 							"cursor": "default"
 						}
 					};
 				}
 			},
 			{
 				field: 'columnComment',
 				title: '字段描述',
 				width: "10%",
 				formatter: function(value, row, index) {
 					var html = $.common.sprintf(
 						"<input class='form-control' type='text' name='columns[%s].columnComment' maxlength='500' value='%s'>",
 						index, value);
 					return html;
 				}
 			},
 			{
 				field: 'columnType',
 				title: '物理类型',
 				width: "10%",
 				'class': "nodrag",
 				cellStyle: function(value, row, index) {
 					return {
 						css: {
 							"cursor": "default"
 						}
 					};
 				}
 			},
 			{
 				field: 'javaType',
 				title: 'Java类型',
 				width: "10%",
 				formatter: function(value, row, index) {
 					var data = [{
 						index: index,
 						javaType: value
 					}];
 					return $("#javaTypeTpl").tmpl(data).html();
 				}
 			},
 			{
 				field: 'javaField',
 				title: 'Java属性',
 				width: "10%",
 				formatter: function(value, row, index) {
 					var html = $.common.sprintf(
 						"<input class='form-control' type='text' name='columns[%s].javaField' maxlength='200' value='%s' required>",
 						index, value);
 					return html;
 				}
 			},
 			/* {
 			    field: 'isInsert',
 			    title: '插入',
 			    width: "5%",
 			    formatter: function (value, row, index) {
 			    	var isCheck = value == 1 ? 'checked' : '';
 			    	var html = $.common.sprintf("<label class='check-box'><input type='checkbox' name='columns[%s].isInsert' value='1' %s></label>", index, isCheck);
 					return html;
 				}
 			}, */
 			{
 				field: 'isEdit',
 				title: '编辑',
 				width: "5%",
 				formatter: function(value, row, index) {
 					var isCheck = value == 1 ? 'checked' : '';
 					var html = $.common.sprintf(
 						"<label class='check-box'><input type='checkbox' name='columns[%s].isEdit' value='1' %s></label>", index,
 						isCheck);
 					return html;
 				}
 			},
 			{
 				field: 'isList',
 				title: '列表',
 				width: "5%",
 				formatter: function(value, row, index) {
 					var isCheck = value == 1 ? 'checked' : '';
 					var html = $.common.sprintf(
 						"<label class='check-box'><input type='checkbox' name='columns[%s].isList' value='1' %s></label>", index,
 						isCheck);
 					return html;
 				}
 			},
 			{
 				field: 'isQuery',
 				title: '查询',
 				width: "5%",
 				formatter: function(value, row, index) {
 					var isCheck = value == 1 ? 'checked' : '';
 					var html = $.common.sprintf(
 						"<label class='check-box'><input type='checkbox' name='columns[%s].isQuery' value='1' %s></label>", index,
 						isCheck);
 					return html;
 				}
 			},
 			{
 				field: 'queryType',
 				title: '查询方式',
 				width: "10%",
 				formatter: function(value, row, index) {
 					var data = [{
 						index: index,
 						queryType: value
 					}];
 					return $("#queryTypeTpl").tmpl(data).html();
 				}
 			},
 			{
 				field: 'isRequired',
 				title: '必填',
 				width: "5%",
 				formatter: function(value, row, index) {
 					var isCheck = value == 1 ? 'checked' : '';
 					var html = $.common.sprintf(
 						"<label class='check-box'><input type='checkbox' name='columns[%s].isRequired' value='1' %s></label>",
 						index, isCheck);
 					return html;
 				}
 			},
 			{
 				field: 'htmlType',
 				title: '显示类型',
 				width: "12%",
 				formatter: function(value, row, index) {
 					var data = [{
 						index: index,
 						htmlType: value
 					}];
 					return $("#htmlTypeTpl").tmpl(data).html();
 				}
 			},
 			{
 				field: 'dictType',
 				title: '字典类型',
 				width: "13%",
 				formatter: function(value, row, index) {
 					var html = $.common.sprintf(
 						"<input class='form-control' type='text' name='columns[%s].dictType' value='%s' id='columns_dict_%s'>",
 						index, value, row.columnId);
 					return "<div class='input-group'>" + html +
 						"<span class='input-group-addon input-sm' onclick='selectDictTree(" + row.columnId +
 						", this)'><i class='fa fa-search'></i></span></div>";
 				},
 				cellStyle: function(value, row, index) {
 					return {
 						css: {
 							"cursor": "default"
 						}
 					};
 				}
 			}
 		]
 	};
 	$.table.init(options);
 });

// 当所有数据被加载时触发处理函数
function onLoadSuccess(data) {
  try {
    $.fn.select2.defaults.set("theme", "bootstrap");
    $("select.form-control").each(function () {
      $(this).select2().on("change", function () {
        $(this).valid();
      })
    })
  } catch (e) {
    console.log(e)
  }

  $(".check-box").each(function () {
    $(this).iCheck({
      checkboxClass: 'icheckbox-blue'
    })
  })
}

// 当拖拽结束后处理函数
function onReorderRow(data) {
  for (var i = 0; i < data.length; i++) {
    $("#columns_sort_" + data[i].columnId).val(i + 1);
  }
}

$(function () {
  var tplCategory = $("#tplCategory option:selected").val();
  tplCategoryVisible(tplCategory);


	basicDetail(tableId)
});

$('#tplCategory').on('select2:select', function (event) {
  var tplCategory = $(event.target).val();
  tplCategoryVisible(tplCategory);
});

function tplCategoryVisible(tplCategory) {
  if ("crud" == tplCategory) {
    $("#treeCode").select2("val", [""]);
    $("#treeParentCode").select2("val", [""]);
    $("#treeName").select2("val", [""]);
    $("#otherInfo").addClass("hidden");

  } else if ("tree" == tplCategory) {
    $("#otherInfo").removeClass("hidden");
    $("#treeCode").attr("required", "true");
    $("#treeParentCode").attr("required", "true");
    $("#treeName").attr("required", "true");
  }
}

// 选择字典处理函数
function selectDictTree(columnId, obj) {
  var dictType = $.common.nullToStr($(obj).parent().find("input").val());
  var url =  "/pages/sysadm/dict/tree?id=" + columnId + "&type=" + dictType;
  var options = {
    title: '选择字典类型',
    width: "380",
    url: url,
    callBack: doSubmit
  };
  $.modal.openOptions(options);
}

function doSubmit(index, layero) {
  var body = layer.getChildFrame('body', index);
  var columnId = body.find('#columnId').val();
  var dictType = body.find('#dictType').val();
  layer.close(index);
  $("#columns_dict_" + columnId).val(dictType);
}

function basicDetail(id){
	$.operate.get(prefix +'/getById?id=' + id, function (res) {
		res.data.oper = oper
		applyDrawing('#tab-basic', res.data)
		applyDrawing('#tab-gen', res.data)
		tplCategoryVisible(res.data.tplCategory)
		res.data.columns.forEach(function (item) {
			$('#treeCode').append(optionAdd(item))
			$('#treeParentCode').append(optionAdd(item))
			$('#treeName').append(optionAdd(item))
		})
		//给select赋值
		$("#tplCategory").select2("val", [res.data.tplCategory]);
		$('#treeCode').select2("val", [res.data.treeCode])
		$('#treeParentCode').select2("val", [res.data.treeParentCode])
		$('#treeName').select2("val", [res.data.treeName])

	}, false)
}

function optionAdd(item) {
	return '<option value="' + item.columnName + '">' + item.columnName + '：' + item.columnComment + '</option>'
}